package com.javasea.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RedisSpringDataApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Redis {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void List() {
        String[] s = {"1", "2", "6", "7", "8", "4", "5", "9"};
  /*
        List<String> list =Arrays.asList(s);
//        redisTemplate.opsForValue().set("paiming",list,1851000l);
        redisTemplate.delete("paiming");
        redisTemplate.opsForList().leftPush("paiming", list);
        List<List<String>> l = redisTemplate.opsForList().range("paiming", 0, -1);
        for (String s1 : l.get(0)) {
            System.out.println(s1);
        }*/

        Set<String> sets = new HashSet(10);
        for (String s1 : s) {
            sets.add(s1);
        }

        redisTemplate.delete("paiming");
        redisTemplate.opsForSet().add("paiming", sets);
        Set<Set<String>> paiming = redisTemplate.opsForSet().members("paiming");
        Iterator<Set<String>> iterator = paiming.iterator();
        // 正确
   /*     while (iterator.hasNext()){
            List<String> next = (List<String>) iterator.next();
            next.forEach(a->{
                System.out.println(a);
            });
        }*/
        // 错误 -- 死循环
//        while (iterator.hasNext()) {
//            Set<String> next1 = iterator.next();
//            while (next1.iterator().hasNext()) {
//                System.out.println(next1.iterator().next());
//            }
//        }
        // 修改如下
        while (iterator.hasNext()) {
            Set<String> next1 = iterator.next();
            Iterator<String> iterator1 = next1.iterator();
            while (iterator1.hasNext()) {
                System.out.println(iterator1.next());
            }
        }
       /* redisTemplate.delete("paiming");
        redisTemplate.opsForZSet().add("paiming",sets);
        Set paiming = redisTemplate.opsForZSet().range("paiming",0,-1);
        Iterator iterator = paiming.iterator();
        while (iterator.hasNext()){
            List<String> next = (List<String>) iterator.next();
            next.forEach(a->{
                System.out.println(a);
            });
        }*/
    }
}
